/*
	This file is part of Limpet.

    Limpet is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Limpet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Limpet.  If not, see http://www.gnu.org/licenses/.
	
	Copyright 2010 Dan Popick
	
	NOTE: several lines of code were lifted from: 
		http://www.webtoolkit.info/javascript-base64.html	
	Though I optimized the decode by using a lookup array
*/

var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	_charToByte: [
	    0,   0,   0,   0,   0,   0,   0,   0,  
	    0,   0,   0,   0,   0,   0,   0,   0,  
	    0,   0,   0,   0,   0,   0,   0,   0,  
	    0,   0,   0,   0,   0,   0,   0,   0,  
	    0,   0,   0,   0,   0,   0,   0,   0,  
	    0,   0,   0,  62,   0,   0,   0,  63,  
	   52,  53,  54,  55,  56,  57,  58,  59,  
	   60,  61,   0,   0,   0,  64,   0,   0,  
	    0,   0,   1,   2,   3,   4,   5,   6,  
		7,   8,   9,  10,  11,  12,  13,  14,  
	   15,  16,  17,  18,  19,  20,  21,  22,
	   23,  24,  25,   0,   0,   0,   0,   0,
	    0,  26,  27,  28,  29,  30,  31,  32,  
	   33,  34,  35,  36,  37,  38,  39,  40,  
	   41,  42,  43,  44,  45,  46,  47,  48,  
	   49,  50,  51,   0,   0,   0,   0,   0,  
	    0,   0,   0,   0,   0,   0,   0,   0 
	],
 
	// public method for decoding
	decode : function (input) {
		var data = [];
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
			
			enc1 = this._charToByte[input.charCodeAt(i++)];
			enc2 = this._charToByte[input.charCodeAt(i++)];
			enc3 = this._charToByte[input.charCodeAt(i++)];
			enc4 = this._charToByte[input.charCodeAt(i++)];
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			data.push(chr1);
			if (enc3 != 64) data.push(chr2);
			if (enc4 != 64) data.push(chr3);
 		}
 
		return data;
	},
}